package com.yulinlin.lang.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 线程工具
 */
public class ThreadUtil {

    public static ThreadLocal<Map<Object, Object>> threadLocal = new ThreadLocal<Map<Object, Object>>();

    public static void set(Object key, Object value) {
        Map<Object, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static <E> E get(Object key){
        Map<Object, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        return (E)map.get(key);
    }

    public static <E> E del(Object key){
        Map<Object, Object> map = threadLocal.get();
        if (map != null) {
            return (E)map.remove(key);
        }
        return null;
    }

    public static void remove(){
        threadLocal.remove();
    }

}